//
//  ScrollCell.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/12.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "HomeScrollCell.h"

#import "DetailViewController.h"


@interface HomeTopImageButton : UIView
@property(nonatomic,strong)UIImageView * imageView;

@property(nonatomic,strong)UILabel * titleLabel;

@property(nonatomic,strong)UILabel * priceLabel;
@end

@implementation HomeTopImageButton

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
//        self.backgroundColor = [UIColor grayColor];
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self addSubview:imageView];
        self.imageView = imageView;
        imageView.image = [UIImage imageNamed:@"localDetailIMage_1"];
        
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        titleLabel.font = [UIFont systemFontOfSize:Ratio_Home_Height(18)];
        titleLabel.numberOfLines = 1;
//        titleLabel.backgroundColor = RandColor;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"卡瑞佳红葡萄酒";
        
        
        UILabel * priceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:priceLabel];
        self.priceLabel = priceLabel;
        priceLabel.text = @"199.00";
//        priceLabel.backgroundColor = RandColor;
        priceLabel.font = [UIFont systemFontOfSize:Ratio_Home_Height(14)];
        priceLabel.textAlignment = NSTextAlignmentCenter;
        priceLabel.textColor = HexColor(@"#F21A1A");
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.imageView.x = 0;
    self.imageView.y = 0;
    self.imageView.width = self.width;
    self.imageView.height = self.imageView.width;
    
    self.titleLabel.y = self.imageView.botton + Ratio_Home_Height(8);
    self.titleLabel.x = 0;
    self.titleLabel.width = self.width;
    self.titleLabel.height = Ratio_Home_Height(14);
    
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %@",self.priceLabel.text];
    self.priceLabel.y = self.titleLabel.botton + Ratio_Home_Height(4);
    self.priceLabel.x = 0;
    self.priceLabel.width = self.width;
    self.priceLabel.height = Ratio_Home_Height(12);
}
@end


@implementation HomeScrollCell

-(void)awakeFromNib{
    [super awakeFromNib];
    self.contentScrollView.backgroundColor = [UIColor whiteColor];
    
    self.cellHeight = Ratio_Home_Height((240.f));
    self.contentScrollView.height = self.cellHeight;
    for (UIView * v in self.contentScrollView.subviews) {
        [v removeFromSuperview];
    }
    
    CGFloat space = Ratio_Home_Width(11.f);
    CGFloat width = Ratio_Home_Width(167.f);
    
    for (int i =0 ; i<10; i++) {
        HomeTopImageButton * button = [[HomeTopImageButton alloc] initWithFrame:CGRectMake(i*(space+width)+space, 0, width, self.cellHeight)];
        [button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ButtonClick:)]];
        [self.contentScrollView addSubview:button];
    }
    
    self.contentScrollView.contentSize = CGSizeMake(10 * (space+width), 0);
}

-(void)setModels:(id)models{
    _models = models;
    for (UIView * v in self.contentScrollView.subviews) {
        [v removeFromSuperview];
    }
    CGFloat space = Ratio_Home_Width(11.f);
    CGFloat width = Ratio_Home_Width(167.f);
    for (int i =0 ; i<[_models count]; i++) {
        HomeTopImageButton * button = [[HomeTopImageButton alloc] initWithFrame:CGRectMake(i*(space+width)+space, 0, width, self.cellHeight)];
        [button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ButtonClick:)]];
        button.tag = i;
        [self.contentScrollView addSubview:button];
        
        Product * product = [_models objectAtIndex:i];
        button.titleLabel.text = product.name;
        button.priceLabel.text = product.price;
        [button.imageView sd_setImageWithURL:[NSURL URLWithString:product.small_pic] placeholderImage:nil];
    }

    self.contentScrollView.contentSize = CGSizeMake([_models count] * (space+width), 0);
}
-(void)ButtonClick:(UITapGestureRecognizer*)button{
    HomeTopImageButton * btn = (HomeTopImageButton*)button.view;
    
    CGRect rect = [btn convertRect:btn.bounds toView:self.window];
    
    CGRect rect1 = [self.contentView convertRect:self.contentView.bounds toView:self.window];
    CGPoint leftTop = CGPointMake(rect.origin.x>100?rect.origin.x:rect.origin.x, rect1.origin.y);

    DetailViewController * detailVC = [[DetailViewController alloc] init];
    detailVC.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftTop.x, leftTop.y, btn.width, btn.width)];
    detailVC.imageView.image = btn.imageView.image;
    detailVC.starPoint = leftTop;
    [[LXDataManager manager] getProductDetailWithProduct_id:[[self.models objectAtIndex:btn.tag] product_id] complete:^(BOOL success, id res) {
        detailVC.product = res;
        CircleAnimator * animator = [CircleAnimator animatorWithFromImageView:btn.imageView toImageView:detailVC.imageView];
        detailVC.animator = animator;
        [[self getCurrentVC].navigationController xw_pushViewController:detailVC withAnimator:animator];
    }];
    
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
    
}
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
}
@end


